<template>
  <div class="login">
        <el-form :model="userinfo" status-icon :rules="rules" ref="userLogin" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="userinfo.username" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="pwd">
                <el-input type="password" v-model="userinfo.pwd" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
  </div>
</template>

<script>
import Cookies from 'js-cookie';
import Ajax from "../ajax";

export default {
  name: 'Login',
  data() {
        return {
            userinfo: {
                username: '',
                pwd: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                pwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        }
  },
  methods: {
        submitForm() {
            this.$refs.userLogin.validate((valid) => {
                if (valid) {
                    Ajax({
                        url: '/api/admin/login',
                        data: this.userinfo,
                        success: (result) => {
                            Cookies.set('token', result.token, { path: '/' });
                            Cookies.set('name', result.dto.username, { path: '/' });
                            this.$router.push('/home');
                        },
                        error: (error) => {
                            this.$message.error(error || '登录失败');
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.userLogin.resetFields();
        }
  }
}
</script>


<style scoped>
    
</style>